package com.wisemoresystem.seal.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wisemoresystem.seal.base.service.BaseService;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_bank_level;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.util.InTools;
import com.wisemoresystem.seal.util.SpringTools;
import com.wisemoresystem.seal.web.WebTools;

@Service
public class BankService extends BaseService {

	// 所有同级菜单
	public List<String> selectUbank(String bank_id) {

		List<String> ids = new ArrayList<String>();

		List<Map<String, Object>> bankList = selectBankBySuperId(bank_id);
		for(Map<String, Object> map:bankList){
			if (bank_id.equals(map.get("_id") + "")) {
			} else {
				ids.add(map.get("_id") + "");
			}
		}
		
		return ids;
	}

	// 循环查询当前用户的下属机构,包括自己
	public String selectUbank2() {
		List<String> list = selectBankSuperIds(WebTools.getCurrentBankId());
		list.add(WebTools.getCurrentBankId());
		String ids = InTools.listToString(list);
		
		return ids;
	}

	public List<Map<String, Object>> selectBankBySuperId(String super_id) {
		String sql = "select * from t_bank t where t.status=0 and t.super_id=? order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, super_id);
		return list;
	}

	// 查询当前用户的bank菜单以及下属机构
	public List<Map<String, Object>> selectUserBank() {

		List<Map<String, Object>> list = selectBankAllListBySuperId(WebTools.getCurrentBankId());
		
		return list;
	}

	// 查询当前用户的bank除本身菜单以及下属机构
	public List<Map<String, Object>> selectUserBankList() {

		List<Map<String, Object>> list = selectBankListBySuperId(WebTools.getCurrentBankId());
		
		return list;
	}

	// 查询当前用户的bank菜单除本身同级菜单属机构
	public List<Map<String, Object>> selectUserSameBank() {

		t_user user = super.findObjById(WebTools.getCurrentUserId(),
				t_user.class);
		t_bank bank = super.findObjById(user.getBank_id(), t_bank.class);

		List<String> sid = selectUbank(bank.getSuper_id());
		String ids = InTools.listToString(sid);;

		String sql = "select * from t_bank t where t.status=0 and t._id in (@ids) and t._id!=? order by t.name asc ";
		sql = sql.replace("@ids", ids);
		List<Map<String, Object>> list = jt.queryForList(sql, bank.get_id());
		return list;
	}

	// 所有机构菜单
	public List<Map<String, Object>> selectBank() {
		String sql = "select * from t_bank t where t.status=0 order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql);
		return list;
	}

	// 获取下级别菜单
	public List<Map<String, Object>> selectLowerBank() {
		return selectLowerBank(WebTools.getCurrentBankId());
	}

	public List<Map<String, Object>> selectLowerBank(String bank_id) {
		String sql = "select * from t_bank t where t.super_id=? and t.status=0 order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, bank_id);
		return list;
	}
	
	public String selectSuperBankId(String bank_id) {
		String sql = "select * from t_bank t where t._id=? and t.status=0 order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, bank_id);
		System.err.println("========="+list.size());
		String super_id = list.get(0).get("super_id").toString();
		return super_id;
	}

	// 获取网点别菜单
	public List<Map<String, Object>> selectNetBank(String bank_id) {
		String sql = "select * from t_bank t where t.level_id='4' and t._id!=? and t.status=0 order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, bank_id);
		return list;
	}

	// 获取级别相同及之上别菜单
	public List<Map<String, Object>> selectLevelBank(String bank_id) {

		int lev = getBankLevel(bank_id);
		t_bank t = super.findObjById(bank_id, t_bank.class);

		String sql = "select t.* from t_bank t "
				+ " left join t_bank_level tl on t.level_id=tl._id "
				+ " where tl.sort<? and t._id!=? and t.status=0 order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql, lev,
				t.getSuper_id());

		return list;
	}

	// 获取上级级别菜单
	public List<Map<String, Object>> selectSuperBank() {
		String sql = "select * from t_bank t where t.status=0 order by t.name asc ";
		List<Map<String, Object>> list = jt.queryForList(sql,
				WebTools.getCurrentBankId());
		return list;
	}

	@Transactional
	public void saveBank(List<Map<String, Object>> list) {

		for (Map<String, Object> map : list) {
			String super_code = map.get("super_code") + "";
			t_bank t = selectBankByCode(super_code);
			if (ChkTools.isNotNull(t)) {
				t_bank bank = selectBankByCode(map.get("code") + "");
				if (ChkTools.isNotNull(bank)) {
					bank.setName(map.get("name") + "");
					bank.setSuper_id(t.get_id());
					super.updateObj(bank);
				} else {
					bank = new t_bank();
					bank.setCode(map.get("code") + "");
					bank.setName(map.get("name") + "");
					bank.setSuper_id(t.get_id());
					bank.setUser_id(WebTools.getCurrentUserId());
					super.saveObj(bank);
				}
			} else {
				t = new t_bank();
				t.setCode(map.get("super_code") + "");
				t.setUser_id(WebTools.getCurrentUserId());
				t.setSuper_id("root");
				super.saveObj(t);
				t_bank bank = new t_bank();
				bank.setCode(map.get("code") + "");
				bank.setName(map.get("name") + "");
				bank.setUser_id(WebTools.getCurrentUserId());
				bank.setSuper_id(t.get_id());
				super.saveObj(bank);
			}
		}
	}

	public t_bank selectBankByCode(String code) {
		String hql = "from t_bank t where t.code=? and t.status=0";
		t_bank t = super.findObj(hql, code);
		return t;
	}

	// 获取银行列表
	public List<Map<String, Object>> selectBankLevel() {
		String sql = "select * from t_bank_level t where t.status=0 order by t.sort asc ";
		List<Map<String, Object>> list = jt.queryForList(sql);
		return list;
	}

	// 获取机构级别
	public int getBankLevel(String bank_id) {
		t_bank b1 = super.findObjById(bank_id, t_bank.class);
		t_bank b2 = super.findObjById(b1.getSuper_id(), t_bank.class);
		if (ChkTools.isNull(b2))
			return 1;// 总行
		t_bank b3 = super.findObjById(b2.getSuper_id(), t_bank.class);
		if (ChkTools.isNull(b3))
			return 2;// 一级分行
		t_bank b4 = super.findObjById(b3.getSuper_id(), t_bank.class);
		if (ChkTools.isNull(b4))
			return 3;// 二级分行
		t_bank b5 = super.findObjById(b4.getSuper_id(), t_bank.class);
		if (ChkTools.isNull(b5))
			return 4;// 支行
		t_bank b6 = super.findObjById(b5.getSuper_id(), t_bank.class);
		if (ChkTools.isNull(b6))
			return 5;// 网点
		return 6;// 分处理处
	}

	// bank编码唯一性
	public boolean checkUniqueCode(String bank_id, String code) {
		String sql = "select * from t_bank t where t.code=? and t._id!=? and t.status=0 ";
		List<String> params = new ArrayList<String>();
		params.add(code);
		if (ChkTools.isNull(bank_id)) {
			sql = sql.replace("and t._id!=?", "");
		} else {
			params.add(bank_id);
		}
		List<Map<String, Object>> list = jt.queryForList(sql, params.toArray());
		if (list.size() > 0)
			return false;
		return true;
	}

	/**
	 * 处理子级机构
	 * 
	 * @param bank_id
	 * @param lv
	 *            机构级别变更差值
	 * @return
	 */
	@Transactional
	public boolean updateLowerBank(String bank_id, int lv) {

		// 查询当前用户的bank除本身菜单以及下属机构
		String ids = selectUbank2(bank_id);
		if (ChkTools.isNull(ids) || lv == 0)
			return true;

		String[] allids = ids.split(",");

		for (String id : allids) {
			if (ChkTools.isNull(id))
				continue;
			List<Map<String, Object>> list = selectBankLevel();
			t_bank bank = super.findObjById(id, t_bank.class);
			t_bank_level level = super.findObjById(bank.getLevel_id(),
					t_bank_level.class);
			bank.setLevel_id(list.get(level.getSort() - lv - 1).get("_id") + "");
			super.updateObj(bank);
		}

		return true;
	}

	// 循环查询除本身下属机构
	public String selectUbank2(String bank_id) {
		List<String> list = selectBankSuperIds(bank_id);
		String ids = InTools.listToString(list);
		
		return ids;
	}

	/**
	 * 查询下级 及其 所有子 下级(有封装)
	 * 
	 * @param bank_id
	 * @return
	 */
	public List<Map<String, Object>> selectBanksBySuperId(String bank_id) {
		List<Map<String, Object>> subBanks = selectLowerBank(bank_id);
		for (Map<String, Object> map : subBanks) {
			String _id = (String) map.get("_id");
			List<Map<String, Object>> subOBanks = selectBanksBySuperId(_id);
			map.put("subs", subOBanks);
		}
		return subBanks;
	}
	
	/**
	 * 查询下级 及其 所有子 下级
	 * 
	 * @param bank_id
	 * @return
	 */
	public List<Map<String, Object>> selectBankListBySuperId(String bank_id) {
		List<Map<String, Object>> subBanks = selectLowerBank(bank_id);

		List<Map<String, Object>> subSubBanks = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> map : subBanks) {
			String _id = (String) map.get("_id");
			List<Map<String, Object>> subSubSubBanks = selectBankListBySuperId(_id);
			subSubBanks.addAll(subSubSubBanks);
		}

		subBanks.addAll(subSubBanks);
		return subBanks;
	}
	
	/**
	 * 查询下级 及其 所有子 下级,包括自己
	 * 
	 * @param bank_id
	 * @return
	 */
	public List<Map<String, Object>> selectBankAllListBySuperId(String bank_id) {
		
		String sql = "select * from t_bank where _id=?";
		List<Map<String, Object>> subBanks = jt.queryForList(sql, bank_id);
		
		List<Map<String, Object>> list = selectBankListBySuperId(bank_id);
		subBanks.addAll(list);
		
		return subBanks;
	}
	
	/**
	 * 获取当前以及当前一下所有机构id
	 * @param bank_id
	 * @return
	 */
	public List<String> selectBankSuperIds(String bank_id) {
		List<Map<String, Object>> subBanks = selectLowerBank(bank_id);
		
		List<String>  slist = new ArrayList<String>();
		for (Map<String, Object> map : subBanks) {
			String _id = (String) map.get("_id");
			slist.add(_id);
		}
		
		for (Map<String, Object> map : subBanks) {
			String _id = (String) map.get("_id");
			List<String> subBanks_id = selectBankSuperIds(_id);
			slist.addAll(subBanks_id);
		}
		return slist;
	}

	public static void main(String[] args) {
		BankService s = SpringTools.getBean(BankService.class);
//		System.err.println(s.selectBanksBySuperId("root").size());
		System.err.println(s.selectBankSuperIds("root").size());
//		System.err.println(s.selectBankSuperIds("root").size());
	}
}
